package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        category.setCreateUser(empId);
        category.setUpdateUser(empId);

        categoryService.save(category);

        return R.success("success");
    }

    @GetMapping("/page")
    public R<Page<Category>> findByPage(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        R<Page<Category>> resultPage = categoryService.findByPage(page, pageSize);

        return resultPage;
    }

    @DeleteMapping
    public R<String> delete(Long id) {
        categoryService.delete(id);
        return R.success("success");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        category.setUpdateUser(empId);

        categoryService.update(category);
        return R.success("success");
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //注意：如果是要做下载处理，必须通知浏览器处理文件的内容以文件下载的形式去处理
        response.setHeader("content-disposition","attachment;filename=category.xlsx"); //设置响应头

        //读取excel模板文件输入流
        InputStream inputStream = CategoryController.class.getResourceAsStream("/excel/base.xlsx");

        //使用输入流创建工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);

        //通过工作簿得到工作单
        Sheet sheet = workbook.getSheetAt(0);

        //得到第一行
        Row row = sheet.getRow(1);

        List<CellStyle> cellStyleList = new ArrayList<>();

        //得到第一行的内容，提取第一行的样式存储到集合中
        for (int i = 0; i < 5; i++) {
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStyleList.add(cellStyle);
        }

        //查询所有的类别信息，得到List集合。
        List<Category> categoryList = categoryService.findAll();
        //遍历所有的类别信息，每一个类别信息对应一行内容。
        for (int i = 0; i < categoryList.size(); i++) {
            Category category = categoryList.get(i);
            //每一个类别信息对应一行内容。
            row = sheet.createRow(i + 1);

            Cell cell = row.createCell(0);
            cell.setCellValue(i + 1);
            cell.setCellStyle(cellStyleList.get(0));

            cell = row.createCell(1);
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStyleList.get(1));

            cell = row.createCell(2);
            cell.setCellValue(category.getType() == 1 ? "菜品分类" : "套餐分类");
            cell.setCellStyle(cellStyleList.get(2));

            cell = row.createCell(3);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));
            cell.setCellStyle(cellStyleList.get(3));

            cell = row.createCell(4);
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStyleList.get(4));
        }

        workbook.write(response.getOutputStream());

    }

    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        R<List<Category>> result = categoryService.list(type);
        return result;
    }
}
